﻿using EasyERP_Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyERP_DAL.Base
{
    /// <summary>
    /// 税率管理数据访问类
    /// </summary>
    public class B_Tax_DAL
    {
        #region 获取税率表信息
        /// <summary>
        /// 获取税率表信息
        /// </summary>
        /// <returns>返回查询表</returns>
        public DataTable Get_B_Tax()
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                SELECT tax_cd,
                   tax_nm,
                   tax_rate,
                   remark
                FROM dbo.b_tax;
            ");

            return DbHelperSQL.Query(sb.ToString()).Tables[0];
        }
        #endregion

        #region 添加税率信息
        /// <summary>
        /// 添加税率信息
        /// </summary>
        /// <param name="model">税率实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Insert_B_Tax(B_Tax model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                INSERT INTO dbo.b_tax
                (
                    tax_cd,
                    tax_nm,
                    tax_rate,
                    remark,
                    ins_user,
                    ins_date,
                    up_user,
                    up_date
                )
                VALUES
                (   N'{model.tax_cd}',
                    N'{model.tax_nm}',
                    {model.tax_rate},
                    N'{model.remark}',
                    N'{model.user_cd}',
                    GETDATE(),
                    N'{model.user_cd}',
                    GETDATE()
                );
            ");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 删除税率信息
        /// <summary>
        /// 删除税率信息
        /// </summary>
        /// <param name="model">税率实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Delete_B_Tax(B_Tax model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                DELETE FROM dbo.b_tax
                WHERE tax_cd = N'{model.tax_cd}';
            ");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 修改税率信息
        /// <summary>
        /// 修改税率信息
        /// </summary>
        /// <param name="model">税率实体对象</param>
        /// <returns>返回True：成功；返回False：失败</returns>
        public bool Update_B_Tax(B_Tax model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                UPDATE dbo.b_tax
                SET tax_nm = N'{model.tax_nm}',
                    tax_rate = {model.tax_rate},
                    remark = N'{model.remark}',
                    up_user = N'{model.user_cd}',
                    up_date = GETDATE()
                WHERE tax_cd = N'{model.tax_cd}';
             ");

            return DbHelperSQL.ExecuteSql(sb.ToString()) > 0;
        }
        #endregion

        #region 查询数据是否存在
        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="model">税率实体对象</param>
        /// <returns>返回True：存在；返回False：不存在</returns>
        public bool Exists(B_Tax model)
        {
            //sql语句
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat($@"
                SELECT 1
                FROM dbo.b_tax
                WHERE tax_cd = N'{model.tax_cd}';
            ");

            return DbHelperSQL.Exists(sb.ToString());
        }
        #endregion
    }
}
